package com.youbug.mall.order_oms.feign.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youbug.common.to.OrderItemTo;
import com.youbug.common.to.OrderTo;
import com.youbug.common.vo.QueryVo;
import com.youbug.common.vo.R;
import com.youbug.mall.order_oms.entity.OmsOrder;
import com.youbug.mall.order_oms.entity.OmsOrderItem;
import com.youbug.mall.order_oms.service.IOmsOrderItemService;
import com.youbug.mall.order_oms.service.IOmsOrderService;
import com.youbug.mall.service_ucs.feign.IUcsOrderFeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class UcsOrderFeignServiceImpl implements IUcsOrderFeignService {

    @Autowired
    private IOmsOrderService omsOrderService;
    @Autowired
    private IOmsOrderItemService omsOrderItemService;

    @Override
    public IPage<OrderTo> getOrderList(QueryVo<OrderTo> queryVo) {

        QueryVo<OmsOrder> vo = new QueryVo<>();

        OmsOrder omsOrder = new OmsOrder();

        OrderTo orderTo = queryVo.getPayload();

        BeanUtils.copyProperties(orderTo, omsOrder);

        BeanUtils.copyProperties(queryVo, vo);

        vo.setPayload(omsOrder);
        Page<OrderTo> page = null;
        IPage<OmsOrder> orderIPage = omsOrderService.getOmsOrderPage(vo);
        List<OmsOrder> orders = orderIPage.getRecords();
        if(!CollectionUtils.isEmpty(orders)){
            List<OrderTo> tos = orderIPage.getRecords().stream().map(e -> {
                OrderTo to = new OrderTo();
                BeanUtils.copyProperties(e, to);
                return to;
            }).collect(Collectors.toList());
            page = new Page<>();

            BeanUtils.copyProperties(orderIPage, page);
            page.setRecords(tos);

            List<String> orderSns = tos.stream().map(OrderTo::getOrderSn).collect(Collectors.toList());

            List<OmsOrderItem> item = omsOrderItemService.getOmsOrderItemByOrderSns(orderSns);

            if(!CollectionUtils.isEmpty(item)){
                Map<String, List<OrderItemTo>> listMap = item.stream().map(e->{
                    OrderItemTo itemTo = new OrderItemTo();

                    BeanUtils.copyProperties(e, itemTo);

                    return itemTo;
                }).collect(Collectors.groupingBy(OrderItemTo::getOrderSn));

                tos.forEach(e->{
                    List<OrderItemTo> itemTos = listMap.get(e.getOrderSn());
                    if(!CollectionUtils.isEmpty(itemTos)){
                        e.setOrderItemListTemp(itemTos);
                    }
                });
            }
        }
        return page;
    }

    @Override
    public OrderTo getOrderInfo(String orderSn, String skuId) {
        OmsOrder order = omsOrderService.getOne(new QueryWrapper<OmsOrder>().eq("order_sn", orderSn));

        if(order != null){
            OrderTo to = new OrderTo();
            BeanUtils.copyProperties(order, to);
            if(skuId != null){
                OmsOrderItem orderItem = omsOrderItemService.getOne(new QueryWrapper<OmsOrderItem>()
                        .eq("order_sn", orderSn).eq("sku_id", skuId));
                if(orderItem != null){
                    OrderItemTo itemTo = new OrderItemTo();
                    BeanUtils.copyProperties(orderItem, itemTo);
                    to.setOrderItemListTemp(Collections.singletonList(itemTo));
                }
            }
            return to;
        }
        return null;
    }
}
